package com.example.lostandfoundsystem.services.system;

import com.example.lostandfoundsystem.models.system.*;
import gaarason.database.contract.eloquent.Record;
import gaarason.database.contract.eloquent.RecordList;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lucifer
 * @since 2024-07-21-20:11
 */
@Service
public class AdminRolePermissionService {

    private static Admin.Model admimModel;

    private static Role.Model roleModel;

    private static Permission.Model permissionModel;

    private static AdminRole.Model adminRoleModel;

    private static RolePermission.Model rolePermissionModel;

    // 添加管理员角色关联
    public static void insertAdminRole(String admin, String role) {
        Record<AdminRole, Long> record = adminRoleModel.withTrashed().where("admin_id", admin).where("role_id", role).first();
        if (record != null) {
            record.restore();
            record.save();
        } else {
            AdminRole adminRole = adminRoleModel.newRecord().getEntity();
            adminRole.setAdminId(admin);
            adminRole.setRoleId(role);
        }
    }

    // 插入一条管理员和角色的关联记录
    public static void insertAdminRole(Admin admin, Role role) {
        insertAdminRole(String.valueOf(admin.getId()), String.valueOf(role.getId()));
    }

    // 插入一条角色和权限的关联记录
    public static void insertRolePermission(String role, String permission) {
        Record<RolePermission, Long> record = rolePermissionModel.withTrashed().where("role_id", role).where("permission_id", permission).first();
        if (record != null) {
            record.restore();
            record.save();
        } else {
            RolePermission rolePermission = rolePermissionModel.newRecord().getEntity();
            rolePermission.setRoleId(role);
            rolePermission.setPermissionId(permission);
        }
    }

    // 插入一条角色和权限的关联记录
    public static void insertRolePermission(Role role, Permission permission) {
        insertAdminRole(String.valueOf(role.getId()), String.valueOf(permission.getId()));
    }

    // 删除一条管理员和角色的关联记录
    public static void deleteAdminRole(String admin, String role) {
        adminRoleModel.newQuery().where("admin_id", admin).where("role_id", role).delete();
    }

    // 删除一条管理员和角色的关联记录
    public static void deleteAdminRole(Admin admin, Role role) {
        deleteAdminRole(String.valueOf(admin.getId()), String.valueOf(role.getId()));
    }

    public static void deleteRolePermission(String role, String permission) {
        rolePermissionModel.newQuery().where("role_id", role).where("permission_id", permission).delete();
    }

    // 删除一条角色和权限的关联记录
    public static void deleteRolePermission(Role role, Permission permission) {
        deleteRolePermission(String.valueOf(role.getId()), String.valueOf(permission.getId()));
    }

    // 获得admin所有的role的id
    public static List<String> getAdminRole(Admin admin) {
        //先查出所有和这个admin有关联的role
        RecordList<AdminRole, Long> record = adminRoleModel.newQuery().where("admin_id", admin.getId()).get();
        //然后装载为role对象
        List<String> roleIdList = new ArrayList<>();
        for (Record<AdminRole, Long> adminRole : record) {
            roleIdList.add(adminRole.getEntity().getRoleId());
        }
        return roleIdList.stream().distinct().collect(Collectors.toList());
    }

    // 获得role所有的admin的id
    public static List<String> getRoleAdmin(Role role) {
        RecordList<AdminRole, Long> record = adminRoleModel.newQuery().where("role_id", role.getId()).get();
        List<String> adminIdList = new ArrayList<>();
        for (Record<AdminRole, Long> adminRole : record) {
            adminIdList.add(adminRole.getEntity().getAdminId());
        }
        return adminIdList.stream().distinct().collect(Collectors.toList());
    }

    // 获得permission所有的role的id
    public static List<String> getPermissionRole(Permission permission) {
        RecordList<RolePermission, Long> record = rolePermissionModel.newQuery().where("permission_id", permission.getId()).get();
        List<String> roleIdList = new ArrayList<>();
        for (Record<RolePermission, Long> RolePermission : record) {
            roleIdList.add(RolePermission.getEntity().getRoleId());
        }
        return roleIdList.stream().distinct().collect(Collectors.toList());
    }

    // 获得role所有的permission的id
    public static List<String> getRolePermission(Role role) {
        RecordList<RolePermission, Long> record = rolePermissionModel.newQuery().where("role_id", role.getId()).get();
        List<String> permissionIdList = new ArrayList<>();
        for (Record<RolePermission, Long> RolePermission : record) {
            permissionIdList.add(RolePermission.getEntity().getPermissionId());
        }
        return permissionIdList.stream().distinct().collect(Collectors.toList());
    }

    public static List<String> getAdminPermission(Admin admin) {
        List<String> roleIdList = getAdminRole(admin);
        List<String> permissionIdList = new ArrayList<>();
        for (String s : roleIdList) {
            //把每个角色映射到角色模型
            Record<Role, Long> role = roleModel.newQuery().find(s);
            if (role != null) {
                //如果角色模型不为空，则使用角色模型查权限
                List<String> pIdList = getRolePermission(role.getEntity());
                permissionIdList.addAll(pIdList);
            }
        }
        return permissionIdList.stream().distinct().collect(Collectors.toList());
    }

    public static List<String> getPermissionAdmin(Permission permission) {
        List<String> roleIdList = getPermissionRole(permission);
        List<String> adminIdList = new ArrayList<>();
        for (String s : roleIdList) {
            //把每个角色映射到角色模型
            Record<Role, Long> role = roleModel.newQuery().find(s);
            if (role != null) {
                //如果角色模型不为空，则使用角色模型查权限
                List<String> aIdList = getRoleAdmin(role.getEntity());
                adminIdList.addAll(aIdList);
            }
        }
        return adminIdList.stream().distinct().collect(Collectors.toList());
    }
}
